/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.form;

import dao.ChiTietPhieuNhapHangDAO;
import dao.MatHangDAO;
import dao.NhanVienDAO;
import dao.PhieuNhapHangDAO;
import dao.QuanLySuThayDoiDAO;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Chitietphieunhaphang;
import pojo.Mathang;
import pojo.Nhavien;
import pojo.Phieunhaphang;

/**
 *
 * @author FT
 */
public class CapNhatPhieuNhap extends javax.swing.JFrame {

    /**
     * Creates new form CapNhatPhieuNhap
     */
    private DefaultComboBoxModel cbModel;//status
    private DefaultComboBoxModel cbModel2;//nhanvien
    private DefaultComboBoxModel cbModelMatHang;
    private DefaultTableModel tableModel;
    int stt;
    List<Nhavien> listnv = new ArrayList<Nhavien>();
    List<Mathang> listmh = new ArrayList<Mathang>();
    List<Chitietphieunhaphang> listctpnh = new ArrayList<Chitietphieunhaphang>();

    public String MaTang(String mabandau) {
        String chuoi = "";
        String index = mabandau.substring(3);
        try {

            //System.out.println("Index la"+index+"");
            int chiso = Integer.valueOf(index);
            chiso++;

            if (chiso < 10) {
                chuoi += "ctn00" + chiso;
            } else if (chiso >= 10 && chiso < 100) {
                chuoi += "ctn0" + chiso;
            } else if (chiso >= 1000) {
                chuoi += "ctn" + chiso;
            }
        } catch (Exception e) {
            chuoi += "NA";
        }
        return chuoi;
    }

    public CapNhatPhieuNhap() {
        initComponents();
        cbModel = new DefaultComboBoxModel();
        cbModel2 = new DefaultComboBoxModel();
        cbModelMatHang = new DefaultComboBoxModel();
        tableModel = new DefaultTableModel();

        tableModel.addColumn("STT");
        tableModel.addColumn("Ma mat hang");
        tableModel.addColumn("Tên mat hang");
        tableModel.addColumn("So luong");

        jcbStatus.setModel(cbModel);
        jComboBox1.setModel(cbModel2);
        jcbMatHang.setModel(cbModelMatHang);
        jTable2.setModel(tableModel);

        listnv = NhanVienDAO.layDanhSachNhanVien();
        listmh = MatHangDAO.layDanhSachMatHang();
        listctpnh = ChiTietPhieuNhapHangDAO.layDanhSachChiTietPhieuNhapHang();

        stt = 1;


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtfMaPhieu = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtfMaPhieu1 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jcbStatus = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jlbName = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jcbMatHang = new javax.swing.JComboBox();
        jLabel15 = new javax.swing.JLabel();
        JlbTenMatHang = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jbtThem = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jtfSoLuong = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();

        jtfMaPhieu.setFocusable(false);

        jLabel7.setText("NGÀY CẬP NHẬT:");

        jLabel10.setText("DANH SÁCH CÁC MẶT HÀNG ĐÃ NHẬP:");

        jLabel11.setText("DANH SÁCH CÁC MẶT HÀNG ĐÃ NHẬP:");

        jLabel13.setText("MÃ MẶT HÀNG");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton3.setText("Xóa");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("CẬP NHẬT PHIẾU NHẬP");

        jLabel3.setText("MÃ PHIẾU");

        jtfMaPhieu1.setFocusable(false);

        jLabel8.setText("NGÀY CẬP NHẬT:");

        jTextField4.setFocusable(false);

        jLabel5.setText("TÌNH TRẠNG PHIẾU");

        jcbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("NHÂN VIÊN LẬP");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel9.setText("HỌ VÀ TÊN:");

        jlbName.setText("                                    ");

        jLabel12.setText("DANH SÁCH CÁC MẶT HÀNG ĐÃ NHẬP:");

        jLabel14.setText("MÃ MẶT HÀNG");

        jcbMatHang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbMatHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbMatHangActionPerformed(evt);
            }
        });

        jLabel15.setText("TÊN MẶT HÀNG:");

        JlbTenMatHang.setText("                                   ");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jButton4.setText("Xóa");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("CANCEL");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jbtThem.setText("THÊM");
        jbtThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThemActionPerformed(evt);
            }
        });

        jLabel2.setText("SỐ LƯỢNG");

        jtfSoLuong.setText("1");
        jtfSoLuong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfSoLuongActionPerformed(evt);
            }
        });

        jButton6.setText("OK");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel4))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jtfMaPhieu1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                                            .addComponent(jcbStatus, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel14)
                                            .addComponent(jLabel2))
                                        .addGap(28, 28, 28)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jcbMatHang, 0, 80, Short.MAX_VALUE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(0, 0, Short.MAX_VALUE)))))
                                .addGap(31, 31, 31)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jbtThem)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton4))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel8)
                                                .addComponent(jLabel9))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jTextField4)
                                                .addComponent(jlbName, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel15)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(JlbTenMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(113, 113, 113))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5)
                                .addGap(30, 30, 30))))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfMaPhieu1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jcbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jlbName, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(jcbMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15)
                    .addComponent(JlbTenMatHang))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtThem)
                    .addComponent(jButton4)
                    .addComponent(jLabel2)
                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jButton6))
                .addGap(66, 66, 66))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        String manv = (String) jComboBox1.getSelectedItem();
        for (int i = 0; i < listnv.size(); i++) {
            if (manv.equals(listnv.get(i).getIdnhavien())) {
                jlbName.setText(listnv.get(i).getTennhanvien());
            }
        }

    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jcbMatHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbMatHangActionPerformed
        // TODO add your handling code here:
        String mamh = (String) jcbMatHang.getSelectedItem();
        for (int i = 0; i < listmh.size(); i++) {
            if (mamh.equals(listmh.get(i).getIdmathang())) {
                JlbTenMatHang.setText(listmh.get(i).getTenmathang());
            }
        }

    }//GEN-LAST:event_jcbMatHangActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1MouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        int a = jTable2.getSelectedRow();

        if (a == -1) {
            return;
        }
        tableModel.removeRow(a);
        int index = 1;
        for (int i = 0; i < tableModel.getRowCount(); i++) {
            tableModel.setValueAt(index, i, 0);
            index++;
        }
        stt = index;
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        /* if (jcbStatus.getSelectedItem().equals("Not Check")){
         jtfMaPhieu.setText("1");
         }*/

        this.dispose();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
        jtfMaPhieu1.setText(DanhMucPhieuNhap.jLabel4.getText());
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();

        jTextField4.setText(dateFormat.format(date));


        cbModel.addElement("Not Check");
        cbModel.addElement("Checked");

        for (int i = 0; i < listnv.size(); i++) {
            cbModel2.addElement(listnv.get(i).getIdnhavien());
        }
        Phieunhaphang pn = PhieuNhapHangDAO.GetPhieuNhapHang(jtfMaPhieu1.getText());
        
        cbModel2.setSelectedItem(pn.getNhavien().getIdnhavien());

        for (int i = 0; i < listmh.size(); i++) {
            cbModelMatHang.addElement(listmh.get(i).getIdmathang());
        }

        String maphieu = jtfMaPhieu1.getText();
        //System.out.println("abc: " + listctpnh.get(0).getMathang().getIdmathang());
        if (listctpnh.size() != 0) {
            for (int i = 0; i < listctpnh.size(); i++) {
                if (maphieu.equals(listctpnh.get(i).getPhieunhaphang().getIdphieunhaphang())) {
                    Vector vt = new Vector();
                    vt.add(stt);
                    vt.add(listctpnh.get(i).getMathang().getIdmathang());
                    Mathang mh = MatHangDAO.GetMatHang(listctpnh.get(i).getMathang().getIdmathang());
                    vt.add(mh.getTenmathang());
                    vt.add(listctpnh.get(i).getSoluong());
                    tableModel.addRow(vt);
                }
                stt++;
            }
        }


    }//GEN-LAST:event_formWindowActivated

    private void jbtThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThemActionPerformed
        // TODO add your handling code here:
        Vector vt = new Vector();
        String nsx = "";
        String mamh = "";
        mamh = (String) jcbMatHang.getSelectedItem();
        vt.add(stt);
        vt.add(jcbMatHang.getSelectedItem());
        System.out.println(jcbMatHang.getSelectedItem());
        vt.add(JlbTenMatHang.getText());
        vt.add(jtfSoLuong.getText());
        //nsx = MatHangDAO.LayNhaSanXuat(mamh);
        //System.out.println("Nha san xuat "+ nsx);
        //vt.add(nsx);
        tableModel.addRow(vt);
        stt++;
    }//GEN-LAST:event_jbtThemActionPerformed

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
    }//GEN-LAST:event_jTable2MouseClicked

    private void jtfSoLuongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfSoLuongActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfSoLuongActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date ngay = new Date();
        Date ngaycapnhat = new Date();
        String maphieu = "";
        int status = -1;
        int sodong = 0;
        String thongbao = "";
        try {
            // TODO add your handling code here:
            ngay = PhieuNhapHangDAO.GetPhieuNhapHang(jtfMaPhieu1.getText()).getNgaynhap();
            //String ngaychuoi = ""+ngay.getDate()+"/"+ngay.getMonth()+"/"+ngay.getYear()+" "+ngay.getHours()+":"+ngay.getMinutes()+":"+ngay.getSeconds();
            //ngay = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(ngaychuoi);
            maphieu = jtfMaPhieu1.getText();
            String manv = (String) jComboBox1.getSelectedItem();
            status = (int) jcbStatus.getSelectedIndex();
            int sl;
            String chuoingay = jTextField4.getText();
            ngaycapnhat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(chuoingay);

            PhieuNhapHangDAO.Insert2(maphieu, ngay, manv, status, ngaycapnhat);
            String mathaydoi = QuanLySuThayDoiDAO.MaTangTuDong();
            QuanLySuThayDoiDAO.Insert(mathaydoi, ngaycapnhat, maphieu, 2, Login.manhanvien, "phieu nhap");
            String machitietphieu = ChiTietPhieuNhapHangDAO.MaTangTuDong();
            ChiTietPhieuNhapHangDAO.DeleteAll(maphieu);
            //them vao chi tiet phieu nhap
            sodong = tableModel.getRowCount();

            for (int i = 0; i < sodong; i++) {
                String mamathang = (String) tableModel.getValueAt(i, 1);
                Mathang mh = MatHangDAO.GetMatHang(mamathang);
                sl = Integer.valueOf(tableModel.getValueAt(i, 3).toString());
                if (MatHangDAO.KiemTraLuongTon(mh, sl) == 1) {
                    ChiTietPhieuNhapHangDAO.Insert(machitietphieu, maphieu, mamathang, sl, status);
                    machitietphieu = MaTang(machitietphieu);
                } else if (MatHangDAO.KiemTraLuongTon(mh, sl) == 0) {
                    thongbao += "Mat hang: " + mh.getIdmathang() + " khong thoa dieu kien nhap. \n";
                    thongbao += "So luong ton hien tai (" + mh.getSoluongtonkhohientai() + ") < so luong ton kho toi thieu (" + mh.getSoluongtonkhotoithieu() + ")";
                } else if (MatHangDAO.KiemTraLuongTon(mh, sl) == 2) {
                    thongbao += "Mat hang: " + mh.getIdmathang() + " khong thoa dieu kien nhap. \n";
                    thongbao += "So luong ton hien tai (" + mh.getSoluongtonkhohientai() + ") + so luong nhap(" + sl + ") > so luong ton kho toi da (" + mh.getSoluongtonkhotoida() + ")";
                }

            }
        } catch (ParseException ex) {
            Logger.getLogger(PhieuNhap.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Sai dinh dang ngay");

        } finally {
            if (!thongbao.equals("")) {
                JOptionPane.showMessageDialog(null, thongbao);
            }
            this.dispose();
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CapNhatPhieuNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CapNhatPhieuNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CapNhatPhieuNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CapNhatPhieuNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CapNhatPhieuNhap().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel JlbTenMatHang;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JButton jbtThem;
    private javax.swing.JComboBox jcbMatHang;
    private javax.swing.JComboBox jcbStatus;
    private javax.swing.JLabel jlbName;
    private javax.swing.JTextField jtfMaPhieu;
    private javax.swing.JTextField jtfMaPhieu1;
    private javax.swing.JTextField jtfSoLuong;
    // End of variables declaration//GEN-END:variables
}
